<?php
class Core_Model_View {
	
	/**
	 * holds variables assigned to template
	 * 
	 * @var array $pageData
	 */
	private $pageData = array();
	
	/**
	 * holds render status of view
	 * 
	 * @var $renderTemplate
	 */
	private $renderTemplate;
	
	/**
	 * Accept template to load
	 * 
	 * @param string $template
	 * @param string $functionality
	 * @throws Exception
	 */
	public function __construct($template, $functionality){
		
		/**
		 * compose file name
		 * 
		 *@var filename
		 */
		$file = basename(SERVER_ROOT) . "/" . basename($functionality) . "/" . basename("views") . "/" . basename($template) .".php";

		try {
			
			if(file_exists($file)){
				
				/**
				 * trigger render to include file when this model is destroyed
				 * if we render it now, we wouldn't be able to assign variables
				 * to the view!
				 */
				$this->renderTemplate = $file;
				
			}
			
			else {
				throw new Exception("File '<strong>$file</strong>' does not exists!");
			}
		}
		catch(Exception $e){
			
			echo $e->getMessage();
			
		}
	}
	
	/**
	 * Receives assignments from controller and stores in local data array
	 * 
	 * @param string $variable
	 * @param string $value
	 */
	public function assign($variable, $value){
		
		$this->pageData[$variable] = $value;
	}
	
	public function __destruct(){
		
		/**
		 * parse data variables into local variables, so that they render to the view
		 *
		 */
		$pageData = $this->pageData;
		/**
		 * render view
		 */
		include($this->renderTemplate);
	}
}
